package com.yl.applicationsecurity.controller;

import com.github.pagehelper.PageInfo;
import com.yl.applicationsecurity.pojo.KunpengSqlInjection;
import com.yl.applicationsecurity.pojo.Result;
import com.yl.applicationsecurity.pojo.ScaVulnerability;
import com.yl.applicationsecurity.service.KunpengSqlInjectionService;
import com.yl.applicationsecurity.service.ScaVulnerabilityService;
import com.yl.applicationsecurity.service.ScriptStateService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author 鹤仙问鹿仙
 * @program: Application-security
 * @description:
 * @date 2025-04-01 17:06:20
 */
@RestController
@RequestMapping("/scaVulnerability")
public class ScaVulnerabilityController {

    @Autowired
    private ScaVulnerabilityService scaVulnerabilityService;

    @Autowired
    private ScriptStateService scriptStateService;


    @PostMapping("/list")
    public Result list(String[] envArray,Integer[] statusArray, String[] nationSelect, ScaVulnerability ScaVulnerability, @RequestParam(required = false) Date[] startAndEndDate, @RequestParam(defaultValue = "1", value = "pageNum")Integer pageNum
            , @RequestParam(defaultValue = "10", value = "pageSize")Integer pageSize){
        PageInfo<ScaVulnerability> ScaVulnerabilityList = null;

        if(startAndEndDate!=null&&startAndEndDate.length!=2){
            ScaVulnerabilityList = scaVulnerabilityService.findListByScaVulnerability(envArray,statusArray,nationSelect,ScaVulnerability,null,null,pageNum,pageSize);
        }else{
            ScaVulnerabilityList = scaVulnerabilityService.findListByScaVulnerability(envArray,statusArray,nationSelect,ScaVulnerability,startAndEndDate[0],startAndEndDate[1],pageNum,pageSize);
        }
        return Result.success(ScaVulnerabilityList);
    }



    @RequestMapping("/updateStatus")
    public Result updateStatus(HttpServletRequest request, String[]  ids, Integer updateStatus) {


        if(scaVulnerabilityService.updateStatus(  ids, updateStatus)!=0){
            return  Result.success("修改成功");
        }else{
            return  Result.error("修改失败");
        }


    }
    @PostMapping("/insert")
    public Result insert(@RequestBody List<ScaVulnerability> list) {

        scriptStateService.updateDate("SCA危险开源组件管控");

        for (ScaVulnerability scaVulnerability : list) {
            if (StringUtils.isNotEmpty(scaVulnerability.getAppName()) && StringUtils.isNotEmpty(scaVulnerability.getJarName()) && StringUtils.isNotEmpty(scaVulnerability.getVersion())
                    && StringUtils.isNotEmpty(scaVulnerability.getNation()) && StringUtils.isNotEmpty(scaVulnerability.getEnv())&& StringUtils.isNotEmpty(scaVulnerability.getProject())) {
                scaVulnerabilityService.insert(scaVulnerability);
            }
        }
        return  Result.success("添加成功");

    }


}

